<?php
use think\Db;
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
function sendsms($mobile,$content,$templatecode,$type,$arr=[]){
	$accessKeyId = 'LTAIo2Fr2mo1TYN1'; //阿里云申请的 Access Key ID
	$accessKeySecret = 'fWDjCLZsP1t0THcFleHlnX9c1T7dVX'; //阿里云申请的 Access Key Secret
	$sms = new \ali\Alisms();

	$str = $sms->send_verify($mobile, $content,$templatecode,$type,$arr);
	file_put_contents("log.txt",$str);
	$json = json_decode($str,true);
	if($json["Code"]=="OK"){
			return "1";
	}else{
			return "0";
	}
	
}
function httppost($url){  
       $curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_HEADER, 0);
		
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。
		$data = curl_exec($curl);
		curl_close($curl);
		
		return $data;
}
function http_request($url,$data=array()){
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
	// 我们在POST数据哦！
	curl_setopt($ch, CURLOPT_POST, 1);
	// 把post的变量加上
	curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
	$output = curl_exec($ch);
	curl_close($ch);
	return $output;
  }

function sendtempmsg($template_id,$openid,$content,$arr){
    //$manage=D("manage");
    //$config =C('WEIXINPAY_CONFIG');
	$appid= config("appid");

	$secret= config("secret");
  
	$json_token=file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret);
	$access_token=json_decode($json_token,true);
	//获得access_token
	$access_token=$access_token["access_token"];
	
	$template=array(
	'touser'=>$openid,
	'template_id'=>$template_id,
	'url'=>"",
	'topcolor'=>"#000000",
	'data'=>array(
	'first'=>array('value'=>urlencode($content),'color'=>"#000000"),
	'keyword1'=>array('value'=>$arr[0],'color'=>'#000000'),
	'keyword2'=>array('value'=>$arr[1],'color'=>'#000000'),
	'keyword3'=>array('value'=>$arr[2],'color'=>'#000000'),
	'remark'=>array('value'=>urlencode(""),'color'=>'#000000'),
	)
	);

	$json_template=json_encode($template);

	$url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;

	$res= http_request($url,urldecode($json_template));
	return $res;
    
}
function sendtempmsg2($template_id,$openid,$content,$arr){
    //$manage=D("manage");
    //$config =C('WEIXINPAY_CONFIG');
	$appid= config("appid");

	$secret= config("secret");
  
	$json_token=file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret);
	$access_token=json_decode($json_token,true);
	//获得access_token
	$access_token=$access_token["access_token"];
	
	$template=array(
	'touser'=>$openid,
	'template_id'=>$template_id,
	'url'=>"",
	'topcolor'=>"#000000",
	'data'=>array(
	'first'=>array('value'=>urlencode($content),'color'=>"#000000"),
	'keyword1'=>array('value'=>$arr[0],'color'=>'#000000'),
	'keyword2'=>array('value'=>$arr[1],'color'=>'#000000'),
	'keyword3'=>array('value'=>$arr[2],'color'=>'#000000'),
	'keyword4'=>array('value'=>$arr[3],'color'=>'#000000'),
	'keyword5'=>array('value'=>$arr[4],'color'=>'#000000'),
	'remark'=>array('value'=>urlencode(""),'color'=>'#000000'),
	)
	);

	$json_template=json_encode($template);

	$url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;

	$res= http_request($url,urldecode($json_template));
	return $res;
    
}
function sendtempmsg4($template_id,$openid,$content,$arr){
    //$manage=D("manage");
    //$config =C('WEIXINPAY_CONFIG');
	$appid= config("appid");

	$secret= config("secret");
  
	$json_token=file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret);
	$access_token=json_decode($json_token,true);
	//获得access_token
	$access_token=$access_token["access_token"];
	
	$template=array(
	'touser'=>$openid,
	'template_id'=>$template_id,
	'url'=>"",
	'topcolor'=>"#000000",
	'data'=>array(
	'first'=>array('value'=>urlencode($content),'color'=>"#000000"),
	'keyword1'=>array('value'=>$arr[0],'color'=>'#000000'),
	'keyword2'=>array('value'=>$arr[1],'color'=>'#000000'),
	'keyword3'=>array('value'=>$arr[2],'color'=>'#000000'),
	'keyword4'=>array('value'=>$arr[3],'color'=>'#000000'),
	'remark'=>array('value'=>urlencode(""),'color'=>'#000000'),
	)
	);

	$json_template=json_encode($template);

	$url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;

	$res= http_request($url,urldecode($json_template));
	return $res;
    
}
function rand_number($min=1, $max=9999) {
    return sprintf("%0".strlen($max)."d", mt_rand($min,$max));
}
function get_rand_number($start=1,$end=10,$length=4){
    $connt=0;
    $temp=array();
    while($connt<$length){
        $temp[]=rand($start,$end);
        $data=array_unique($temp);
        $connt=count($data);
    }
    sort($data);
    return $data;
}
function loginlog($username,$mechanismname,$mid,$type){
		$data["username"]=$username;
		$data["mechanismname"]=$mechanismname;
		$data["mid"]=$mid;
	
		$data["ip"]= ip();
	
		$data["address"]= getaddress($data["ip"]);
		$data["devicetype"] = $type;
		$data["createtime"]= date('Y-m-d H:i:s',time());
		db("loginlog")->insertGetId($data);

}
function ip() {
    //strcasecmp 比较两个字符，不区分大小写。返回0，>0，<0。
    if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
        $ip = getenv('HTTP_CLIENT_IP');
    } elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
        $ip = getenv('HTTP_X_FORWARDED_FOR');
    } elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
        $ip = getenv('REMOTE_ADDR');
    } elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    $res =  preg_match ( '/[\d\.]{7,15}/', $ip, $matches ) ? $matches [0] : '';
    return $res;
    //dump(phpinfo());//所有PHP配置信息
}
function getaddress($ip){
	$host = "http://ipquery.market.alicloudapi.com";
    $path = "/query";
    $method = "GET";
    $appcode = "3f4ee56f35ff4737b91a42b226017fde";
    $headers = array();
    array_push($headers, "Authorization:APPCODE " . $appcode);
    $querys = "ip=".$ip;
    $bodys = "";
    $url = $host . $path . "?" . $querys;

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_FAILONERROR, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($curl, CURLOPT_HEADER, false);

    if (1 == strpos("$".$host, "https://"))
    {
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    }

	$output = curl_exec($curl);
	curl_close($curl);
	return $output;
}
function utf8_strlen($string = null) {
	// 将字符串分解为单元
	preg_match_all("/./us", $string, $match);
	// 返回单元个数
	return count($match[0]);
}
function getPushUrl($domain, $streamId, $key = null, $time = null){
	if($key && $time){
		$txTime = strtoupper(base_convert(strtotime($time),10,16));
		$txSecret = md5($key.$streamId.$txTime);
		$extStr = "?".http_build_query(array(
			"txSecret"=> $txSecret,
			"txTime"=> $txTime
		));
	}
	return "rtmp://".$domain."/live/".$streamId.(isset($extStr) ? $extStr : "");
}
function getPlayUrl($domain, $streamId){
	return array(
		"rtmp://".$domain."/live/".$streamId,
		"http://".$domain."/live/".$streamId.".flv",
		"http://".$domain."/live/".$streamId.".m3u8"
	);
}
function getplayurl2($domain,$streamId,$key=null,$time=null){
	if($key && $time){
		$txTime = strtoupper(base_convert(strtotime($time),10,16));
		$txSecret = md5($key.$streamId.$txTime);
		$extStr = "?".http_build_query(array(
			"txSecret"=> $txSecret,
			"txTime"=> $txTime
		));
	}
	return array("http://".$domain."/live/".$streamId.".m3u8".(isset($extStr) ? $extStr : ""),
	"rtmp://".$domain."/live/".$streamId."".(isset($extStr) ? $extStr : ""),
	"http://".$domain."/live/".$streamId.".flv".(isset($extStr) ? $extStr : "")
	);

}